using System.Text.Json.Serialization;

namespace McpApi.Models
{
    /// <summary>
    /// 天气相关数据模型
    /// </summary>

    #region 天气API响应模型

    /// <summary>
    /// 当前天气响应
    /// </summary>
    public class CurrentWeatherResponse
    {
        [JsonPropertyName("coord")]
        public Coordinates? Coord { get; set; }

        [JsonPropertyName("weather")]
        public List<WeatherCondition> Weather { get; set; } = new();

        [JsonPropertyName("base")]
        public string Base { get; set; } = string.Empty;

        [JsonPropertyName("main")]
        public MainWeatherData? Main { get; set; }

        [JsonPropertyName("visibility")]
        public int Visibility { get; set; }

        [JsonPropertyName("wind")]
        public WindData? Wind { get; set; }

        [JsonPropertyName("clouds")]
        public CloudData? Clouds { get; set; }

        [JsonPropertyName("dt")]
        public long Dt { get; set; }

        [JsonPropertyName("sys")]
        public SystemData? Sys { get; set; }

        [JsonPropertyName("timezone")]
        public int Timezone { get; set; }

        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("cod")]
        public int Cod { get; set; }
    }

    /// <summary>
    /// 天气预报响应
    /// </summary>
    public class WeatherForecastResponse
    {
        [JsonPropertyName("cod")]
        public string Cod { get; set; } = string.Empty;

        [JsonPropertyName("message")]
        public int Message { get; set; }

        [JsonPropertyName("cnt")]
        public int Cnt { get; set; }

        [JsonPropertyName("list")]
        public List<ForecastItem> List { get; set; } = new();

        [JsonPropertyName("city")]
        public CityInfo? City { get; set; }
    }

    /// <summary>
    /// 预报项目
    /// </summary>
    public class ForecastItem
    {
        [JsonPropertyName("dt")]
        public long Dt { get; set; }

        [JsonPropertyName("main")]
        public MainWeatherData? Main { get; set; }

        [JsonPropertyName("weather")]
        public List<WeatherCondition> Weather { get; set; } = new();

        [JsonPropertyName("clouds")]
        public CloudData? Clouds { get; set; }

        [JsonPropertyName("wind")]
        public WindData? Wind { get; set; }

        [JsonPropertyName("visibility")]
        public int Visibility { get; set; }

        [JsonPropertyName("pop")]
        public double Pop { get; set; } // 降水概率

        [JsonPropertyName("dt_txt")]
        public string DtTxt { get; set; } = string.Empty;
    }

    #endregion

    #region 天气数据子模型

    /// <summary>
    /// 坐标信息
    /// </summary>
    public class Coordinates
    {
        [JsonPropertyName("lon")]
        public double Lon { get; set; }

        [JsonPropertyName("lat")]
        public double Lat { get; set; }
    }

    /// <summary>
    /// 天气状况
    /// </summary>
    public class WeatherCondition
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("main")]
        public string Main { get; set; } = string.Empty;

        [JsonPropertyName("description")]
        public string Description { get; set; } = string.Empty;

        [JsonPropertyName("icon")]
        public string Icon { get; set; } = string.Empty;
    }

    /// <summary>
    /// 主要天气数据
    /// </summary>
    public class MainWeatherData
    {
        [JsonPropertyName("temp")]
        public double Temp { get; set; }

        [JsonPropertyName("feels_like")]
        public double FeelsLike { get; set; }

        [JsonPropertyName("temp_min")]
        public double TempMin { get; set; }

        [JsonPropertyName("temp_max")]
        public double TempMax { get; set; }

        [JsonPropertyName("pressure")]
        public int Pressure { get; set; }

        [JsonPropertyName("humidity")]
        public int Humidity { get; set; }

        [JsonPropertyName("sea_level")]
        public int? SeaLevel { get; set; }

        [JsonPropertyName("grnd_level")]
        public int? GrndLevel { get; set; }
    }

    /// <summary>
    /// 风力数据
    /// </summary>
    public class WindData
    {
        [JsonPropertyName("speed")]
        public double Speed { get; set; }

        [JsonPropertyName("deg")]
        public int Deg { get; set; }

        [JsonPropertyName("gust")]
        public double? Gust { get; set; }
    }

    /// <summary>
    /// 云量数据
    /// </summary>
    public class CloudData
    {
        [JsonPropertyName("all")]
        public int All { get; set; }
    }

    /// <summary>
    /// 系统数据
    /// </summary>
    public class SystemData
    {
        [JsonPropertyName("type")]
        public int Type { get; set; }

        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("country")]
        public string Country { get; set; } = string.Empty;

        [JsonPropertyName("sunrise")]
        public long Sunrise { get; set; }

        [JsonPropertyName("sunset")]
        public long Sunset { get; set; }
    }

    /// <summary>
    /// 城市信息
    /// </summary>
    public class CityInfo
    {
        [JsonPropertyName("id")]
        public int Id { get; set; }

        [JsonPropertyName("name")]
        public string Name { get; set; } = string.Empty;

        [JsonPropertyName("coord")]
        public Coordinates? Coord { get; set; }

        [JsonPropertyName("country")]
        public string Country { get; set; } = string.Empty;

        [JsonPropertyName("population")]
        public int Population { get; set; }

        [JsonPropertyName("timezone")]
        public int Timezone { get; set; }

        [JsonPropertyName("sunrise")]
        public long Sunrise { get; set; }

        [JsonPropertyName("sunset")]
        public long Sunset { get; set; }
    }

    #endregion

    #region 业务模型

    /// <summary>
    /// 天气预警级别
    /// </summary>
    public enum WeatherAlertLevel
    {
        None = 0,
        Low = 1,
        Medium = 2,
        High = 3,
        Critical = 4
    }

    /// <summary>
    /// 天气对MES的影响类型
    /// </summary>
    public enum WeatherImpactType
    {
        None = 0,
        Transportation = 1,      // 运输影响
        Production = 2,          // 生产影响
        Equipment = 3,           // 设备影响
        Safety = 4,              // 安全影响
        Energy = 5,              // 能源影响
        Personnel = 6            // 人员影响
    }

    /// <summary>
    /// 天气预警信息
    /// </summary>
    public class WeatherAlert
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
        public WeatherAlertLevel Level { get; set; }
        public string Title { get; set; } = string.Empty;
        public string Description { get; set; } = string.Empty;
        public List<WeatherImpactType> ImpactTypes { get; set; } = new();
        public List<string> Recommendations { get; set; } = new();
        public DateTime ValidFrom { get; set; }
        public DateTime ValidTo { get; set; }
        public string Location { get; set; } = string.Empty;
    }

    /// <summary>
    /// 简化的天气信息（用于MCP工具返回）
    /// </summary>
    public class SimpleWeatherInfo
    {
        public string Location { get; set; } = string.Empty;
        public DateTime DateTime { get; set; }
        public double Temperature { get; set; }
        public double FeelsLike { get; set; }
        public int Humidity { get; set; }
        public string Condition { get; set; } = string.Empty;
        public string Description { get; set; } = string.Empty;
        public double WindSpeed { get; set; }
        public int WindDirection { get; set; }
        public double? Precipitation { get; set; }
        public int Visibility { get; set; }
        public WeatherAlertLevel AlertLevel { get; set; }
        public List<string> Warnings { get; set; } = new();
    }

    #endregion
}
